<?php

namespace App\Containers\AppSection\Profile\Actions;

use App\Containers\AppSection\File\Tasks\GetUrlFileTask;
use App\Containers\AppSection\Log\Tasks\CreateLogTask;
use App\Containers\AppSection\Profile\Tasks\FindProfileByIdTask;
use App\Containers\AppSection\Profile\Tasks\UpdateProfileTask;
use App\Containers\AppSection\Profile\UI\API\Requests\DeleteProfileAvatarRequest;
use App\Ship\Exceptions\NotFoundException;
use App\Ship\Parents\Actions\Action;

class DeleteProfileAvatarAction extends Action
{
    public function run(DeleteProfileAvatarRequest $request)
    {
        $profile = app(FindProfileByIdTask::class)->run($request->profile()->id);
        if (empty($profile->avatar)) {
            throw new NotFoundException(__('ship::resource.not-found'));
        }

        app(UpdateProfileTask::class)->run($profile->id, ['avatar_id' => null]);

        app(GetUrlFileTask::class)->run($profile->avatar->id);


        app(CreateLogTask::class)->run($request->profile()->id, 'Удаление аватарки', 'profile');
    }
}
